package com.meichis.yndms.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.*;

import com.meichis.mcsappframework.utils.MessageTools;
import com.meichis.yndms.R;
import com.meichis.yndms.vm.SyncBaseDataPresenter;
import com.meichis.yndms.vm.SyncBaseDataPresenter.SyncData;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class SyncBaseDataDialog extends Dialog {
    private Context mContext;
    private LinkedHashMap<SyncData, Integer> mList = null;

    private ImageButton btnOK;
    private ListView listView;
    private TextView txtTitle;
    private Button bt_Save;

    private ItemAddAdapter adapter;
    private SyncBaseDataPresenter presenter;

    public SyncBaseDataDialog(Context context) {
        super(context);
        mContext = context;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setCancelable(false);
        setContentView(R.layout.dialog_multiplechoice);
        try {
            this.dismiss();
        } catch (Exception e) {
        }

        initData();

        txtTitle = findViewById(R.id.txtTitle);
        txtTitle.setText("选择要同步的选项");
        btnOK = findViewById(R.id.btnCancel);
        btnOK.setOnClickListener(v -> SyncBaseDataDialog.this.dismiss());
        bt_Save = findViewById(R.id.bt_Save);
        bt_Save.setText("开始同步");

        listView = findViewById(R.id.lv_list);
        adapter = new ItemAddAdapter();
        listView.setAdapter(adapter);

        bt_Save.setOnClickListener(view -> {
            presenter.setSyncDatas(getChosen());
            presenter.sync();
        });

        findViewById(R.id.cb_chose).setOnClickListener(view -> {
            if (((CheckBox) view).isChecked()) {
                setAllState(1);
            } else {
                setAllState(0);
            }
            notifyDataChanged();
        });

    }

    private void initData() {
        presenter = new SyncBaseDataPresenter(new SyncBaseDataPresenter.SyncBaseDataListener() {
            @Override
            public void onStart() {
                setAllState(1, 2);
                notifyDataChanged();
                bt_Save.setText("同步中...");
                bt_Save.setBackgroundColor(Color.GRAY);
                bt_Save.setClickable(false);
            }

            @Override
            public void onNext(SyncData data) {
                mList.put(data, 3);
                notifyDataChanged();
            }

            @Override
            public void onComplete() {
                MessageTools.showLongToast("同步成功");
                SyncBaseDataDialog.this.dismiss();
                bt_Save.setText("同步完成");
                bt_Save.setClickable(true);
            }
        });

        mList = new LinkedHashMap<>();
        for (SyncData data : presenter.getAllSyncDatas()) {
            mList.put(data, 1);
        }
    }

    public void notifyDataChanged() {
        adapter.notifyDataSetChanged();
    }

    private void setAllState(int state) {
        for (SyncData data : mList.keySet()) {
            mList.put(data, state);
        }
    }

    private void setAllState(int oldState, int newState) {
        for (SyncData data : mList.keySet()) {
            if (mList.get(data) == oldState) {
                mList.put(data, newState);
            }
        }
    }

    private List<SyncData> getChosen() {
        List<SyncData> syncDatas = new ArrayList<>();
        for (SyncData data : mList.keySet()) {
            if (mList.get(data) == 1) {
                syncDatas.add(data);
            }
        }
        return syncDatas;
    }

    public class ItemAddAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            if (mList != null) {
                return mList.size();
            } else {
                return 0;
            }
        }

        @Override
        public Object getItem(int position) {
            if (mList != null) {
                return mList.keySet().toArray()[position];
            } else {
                return null;
            }
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view = null;
            if (convertView == null) {
                view = View.inflate(mContext, R.layout.dialog_multiplechoice_item, null);
            } else {
                view = convertView;
            }
            TextView value = view.findViewById(R.id.tv_value);
            CheckBox chose = view.findViewById(R.id.cb_chose);
            TextView state = view.findViewById(R.id.tv_state);
            SyncData syncData = (SyncData) getItem(position);
            value.setText(syncData.toString());
            switch (mList.get(syncData)) {
                case 0:
                    chose.setVisibility(View.VISIBLE);
                    state.setVisibility(View.GONE);
                    chose.setChecked(false);
                    break;
                case 1:
                    chose.setVisibility(View.VISIBLE);
                    state.setVisibility(View.GONE);
                    chose.setChecked(true);
                    break;
                case 2:
                    chose.setVisibility(View.GONE);
                    state.setVisibility(View.VISIBLE);
                    state.setTextColor(Color.GREEN);
                    state.setText("同步中...");
                    break;
                case 3:
                    chose.setVisibility(View.GONE);
                    state.setVisibility(View.VISIBLE);
                    state.setTextColor(Color.BLACK);
                    state.setText("同步成功");
                    break;
            }
            view.setOnClickListener(view1 -> {
                if (mList.get(getItem(position)) > 1)
                    return;
                mList.put((SyncData) getItem(position), mList.get(getItem(position)) == 0 ? 1 : 0);
                notifyDataSetChanged();
            });
            return view;
        }

    }
}